package org.example.SolutionNew001;

import java.util.*;

public class Hash03 {
    public static void main(String[] args) {
        Hash03 h=new Hash03();
        int[] nums=new int[]{0,3,7,2,5,8,4,6,0,1};
        System.out.println(h.longestConsecutive(nums));
    }

    public int longestConsecutive1(int[] nums) {
        int n=nums.length;
        if(n==0||n==1) return n;
        Set<Integer> set=new HashSet<>();
        for(int i=0;i<nums.length;i++){
            set.add(nums[i]);
        }
        int res=0;
        for(int num:set){
            if(!set.contains(num-1)){
                int count=1;
                int currentNum=num;

                while(set.contains(++currentNum)){
                    count++;
                }
                res=Math.max(res,count);
            }
        }
        return res;
    }

    public int longestConsecutive(int[] nums) {
        int n=nums.length;
        if(n==0||n==1) return n;
        Set<Integer> set=new HashSet<>();
        for(int i=0;i<nums.length;i++){
            set.add(nums[i]);
        }
        int res=0;
        Iterator<Integer> it=set.iterator();
        while(it.hasNext()){
            int ele=it.next();
            int count=1;
            int start=ele;
            it.remove();
            while(set.contains(--start)){
                count++;
                set.remove(start);
            }
            start=ele;
            while(set.contains(++start)){
                count++;
                set.remove(start);
            }
            res=Math.max(res,count);
        }
        return res;
    }
}
